/**
 * Created by thanatos on 15-9-7.
 */

(function ($, angular) {

    var app = angular.module('app', [])

    /*--catalog--*/
    app.controller('catalog', ['$scope', '$http', function ($scope, $http) {
        $scope.getCategories = function () {
            $http.get('/main/catalog').success(function (resp) {
                $scope.categories = resp.categories
            })
            .error(function (err) {
                    console.log('there are a error in /main/catalog')
                })
        }
        $scope.over = function ($event) {
            console.log('over')
            var $target = $($event.target)
            if(!$target.attr('data-index'))
                return
            $('.hover').removeClass('hover')
            $scope.cname = $target.attr('data-index')
            $('li[data-index="'+$scope.cname+'"]').addClass('hover')
            $('.hidden-catalog').addClass('hover')
            if($scope.categories){
                console.log('hoverIndex>>>', $scope.cname)
                $scope.children = $scope.categories[$scope.cname]
                console.log('children >>>')
                console.log($scope.children)
            }
        }
        $scope.leave = function ($event) {
            var $target = $($event.target)
            $('.hover').removeClass('hover')
        }

    }])

    app.controller('searcher', function ($scope) {
        $scope.more = function ($event) {
            var $this = $($event.target)
            var $i = $this.find('i')
            if($i.hasClass('crumb-drop')){
                $i.removeClass('crumb-drop')
                $i.addClass('crumb-up')
                $scope.menu = true
                $('.brand-wrap').addClass('high-brand-wrap')
                return
            }
            if($i.hasClass('crumb-up')){
                $('.brand-wrap>a').each(function () {
                    $(this).removeClass('hide')
                })
                $i.removeClass('crumb-up')
                $i.addClass('crumb-drop')
                $scope.menu = false
                $('.brand-wrap').removeClass('high-brand-wrap')
            }
        }
        $scope.brand4hover = function ($event) {
            var $this = $($event.target)
            var text = $this.data('value')

            console.log('text>>>', text)

            $('ul.horizontal-menu>li').each(function () {
                $(this).removeClass('active')
            })

            $this.addClass('active')

            if(text == '0'){
                $('.brand-wrap>a').each(function () {
                    $(this).removeClass('hide')
                })
            }else{
                $('.brand-wrap>a').each(function () {
                    var $self = $(this)
                    if($self.data('xchar')==text){
                        $self.removeClass('hide')
                    }else{
                        $(this).addClass('hide')
                    }
                })
            }
        }
    })

    /*--carousel--*/
    app.controller('carousel', ['$scope', function ($scope) {

        var detectColor = function () {
            var $self = $('#index-carousel')
            var index = +$self.find('.carousel-indicators>.active').attr('data-slide-to')
            var color = $($self.find('.carousel-inner>.item>img')[index]).attr('data-color')
            $self.parent().parent().css('background-color', color)
        }

        detectColor()

        $('#index-carousel').on('slid.bs.carousel', function () {
            detectColor()
        });
    }])

    /*--suggest retrieval--*/
    app.controller('retrieval', ['$scope', '$http', function ($scope, $http) {

        //表单提交
        $scope.submit = function (field) {
            console.log("field is ", field)
            if(field){
                $scope.qr = field
            }
            if(!$scope.qr || !$scope.qr.replace('/\s+/g', '')) return;
            document.forms['searcher'].elements['qr'].value = $scope.qr
            document.forms['searcher'].submit()
        }

        //智能提示
        $scope.suggest = function () {
            console.log('value is ', $scope.qr)
            if(!$scope.qr){
                $scope.show = false
            }else{
                $http.get('/suggest', {params:{field:$scope.qr}})
                    .success(function (resp) {
                        console.log('suggests value is ', resp)
                        if(resp.suggests.length==0){
                            $scope.show=false
                        }
                        else{
                            $scope.show = true
                            $scope.suggests = resp.suggests
                        }
                    })
                    .error(function (resp) {

                    })
            }
        }
    }])

})(jQuery, angular)